package com.stellar.config;


import com.stellar.config.properties.AuthCodeProperties;
import com.stellar.core.sender.EmailCodeSender;
import com.stellar.core.sender.SmsCodeSender;
import com.stellar.core.service.VerifyCodeService;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Map;

/**
 * @author Jqcc
 * @version 1.0
 * @date 2025/2/13
 */
@Configuration
@EnableConfigurationProperties(AuthCodeProperties.class)
public class AuthCodeAutoConfiguration {

    private final AuthCodeProperties properties;

    public AuthCodeAutoConfiguration(AuthCodeProperties properties) {
        this.properties = properties;
    }

    @Bean
    @ConditionalOnMissingBean
    public SmsCodeSender smsCodeSender() {
        return new DefaultSmsCodeSender(); // 默认空实现（业务方必须覆盖）
    }

    @Bean
    @ConditionalOnMissingBean
    public EmailCodeSender emailCodeSender() {
        return new DefaultEmailCodeSender(); // 默认空实现（业务方必须覆盖）
    }

    @Bean
    public VerifyCodeService verifyCodeService(SmsCodeSender smsCodeSender,
                                               EmailCodeSender emailCodeSender) {
        return new VerifyCodeService(
                smsCodeSender,
                emailCodeSender,
                Math.max(properties.getSmsLength(), properties.getEmailLength()),
                properties.getExpireSeconds()
        );
    }

    // 默认空实现
    private static class DefaultSmsCodeSender implements SmsCodeSender {

        @Override
        public boolean send(String phone, Map<String, String> templateParam) {
            throw new UnsupportedOperationException("请实现 SmsCodeSender 接口并注册为 Bean");
        }
    }

    private static class DefaultEmailCodeSender implements EmailCodeSender {
        @Override
        public void send(String email, String code) {
            throw new UnsupportedOperationException("请实现 EmailCodeSender 接口并注册为 Bean");
        }
    }
}